package com.yfk.web.service.impl;

import com.yfk.web.entity.OAToYHTVO;
import com.yfk.web.service.IYHTService;
import com.yfk.web.util.BIPUtil;
import com.yfk.web.util.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Service
public class YHTServiceImpl implements IYHTService {
    @Autowired
    public OAToYHTVO oaToYHTVO;
    @Override
    public String getThirdLoginCode(String type, String url, String accessToken, String thirdUcId, String userId) throws Exception {

        String loginURL = this.getJoinURL(type, url, "/yonbip/yht/getThirdLoginCode");
        return BIPUtil.getThirdLoginCode(loginURL, accessToken, thirdUcId, userId);
    }

    @Override
    public String getSignLoginURL(String qycode, String type, String baseURL, String thirdUCId, String redirctUrl, String code) throws Exception {
        String encodeURL = URLEncoder.encode(redirctUrl, "UTF-8");
        String loginURL = String.format("%s/cas/thirdOauth2CodeLogin?thirdUCId=%s&code=%s&service=%s", baseURL,
                thirdUCId, code, encodeURL);
        return loginURL;
    }

    @Override
    public String getYHTUserInfoByCode(String userCode,String qyID,String access_token) throws Exception {
        Map queryParams = new HashMap();
        queryParams.put("access_token", access_token);
        queryParams.put("userCode", userCode);
        queryParams.put("qyID", qyID);
        String apiUrl = this.oaToYHTVO.getBaseURL() + this.oaToYHTVO.getGetwayurl()+this.oaToYHTVO.getUserInfo();
        String result = HttpClientUtil.doGet(apiUrl, null, queryParams);
        return result;
    }

    /**
     * 拼接请求的地址
     *
     * @param type
     * @param baseURL
     * @param joinURL
     * @return
     * @throws Exception
     */
    protected String getJoinURL(String type, String baseURL, String joinURL) throws Exception {
        if ("YFK".equals(type)) { // 友费控增加了网关
            return baseURL + "/iuap-api-gateway" + joinURL;
        } else if ("YHT".equals(type)) {
            return baseURL + "/iuap-api-gateway" + joinURL;
        }
        return baseURL + joinURL;
    }
}
